/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Hogshead.Common.h"
#include "../Interfaces/IHObject.h"
#include "../Interfaces/IGameObjectTreeHolder.h"
#include "../Interfaces/IComponent.h"
#include "../Interfaces/IDirector.h"
#include "../Interfaces/IMessageSender.h"
#include "../GameObject.h"
#include "../GUIPanel.h"


namespace Hogshead
{
	namespace Core
	{
		enum ScenePhase 
		{
			None, 
			Loading, 
			Initializing, 
			GameObjectUpdate, 
			DirectorUpdate, 
			MessageHandling, 
			Draw
		};

		enum SceneProcessesCompleted
		{
			NONE = 0,
			LOADED = 1,
			INITIALIZED = 2,
			HAS_BEEN_RUN = 4
		};

		class Scene : public IGameObjectTreeHolder, public IMessageSender
		{
			ContentH(Hogshead::Core::Scene, Scene)
		public:
			Scene();
			~Scene();

			inline ScenePhase getScenePhase() const
			{
				return _scene_phase;
			}

			// called whenever the table dirty flag is set
			void buildGameObjectTable();

			// called by game on each frame
			void update(const GameTimer& in_gt);
			// draw directors which are in the draw list
			static void update_game_object(int, void*);
			void draw();
			// add a director to this scene
			void addDirector(IDirector* in_director, bool is_drawable=false);
			// get a director pointer by type
			IDirector* getDirector(const String& in_name);

			// add a named game object to this scene
			void addGameObject(GameObject* in_game_object);
			/* get back a game object by it's name */
			GameObject* getGameObject(const String& in_name);

			/* 
			* Called when we need a list of the GameObjects in the Scene with a given name
			* @return Returns a pointer to the list of the GameObjects in the Scene with a given name if there 
			* are any, otherwise it returns NULL.
			*/
			Vector<GameObject*>* getGameObjects(const String& in_name);

			// remove a game object from the scene by it's global name
			void RemoveReleaseGameObject(GameObject* in_to_remove);

			// add a named gui panel to this scene
			void addGUIPanel(GUIPanel* in_gui_panel);
			// get back a gui panel by it's name
			GUIPanel* getGUIPanel(const String& in_name);

			// set the root game object
			void setRootGameObject(GameObject* in_game_object);
			// get the root game object
			GameObject* getRootGameObject();

			// set the root gui panel
			void setRootGUIPanel(GUIPanel* in_panel);
			// get the root game object
			GUIPanel* getRootGUIPanel();

			// load a scene from an XML file and return it (uninitialized)
			static Scene* loadScene(const String& in_filename);
			
			// initialize the scene
			void initialize();

			//load a prefab
			//Prefab* getPrefab(const String& in_path);			
			//GameObject* getPrefabInstance(const String& in_name, GameObject* in_to_be_parent = 0);
			//GameObject* getPrefabLoadInstance(const String& in_name, const String& in_path, GameObject* in_to_be_parent);


			// get the pointer to the game project
			IGame* getGame();


			// register director for game messages
			void registerForMessages(IDirector* in_director);
			// register compnent for game messages
			void registerForMessages(IComponent* in_component);

			// notify registered objects that scene is being paused
			void pause();
			// notofiy registered object that scene is being resumed
			void resume();


			void setDirtyLayer(int);

		private:  

			friend class IGame;
			ScenePhase _scene_phase;
			int _scene_processes_completed;

			IGame* _parent_game;
			GameObject* _root_game_object;
			GUIPanel* _root_gui_panel;
			// the table where we store our game objects for updating (each row is a level in the tree)
			Table<GameObject*> _game_object_table;
			// buffer to store our game objects in when putting in the table
			ArrayList<GameObject*> _game_object_buffer;
			bool _table_dirty;
			int _table_dirty_layer;

			Vector<IDirector*> _directors;
			HashTable<String, IDirector*> _director_map;

			/* A mapping from name (string) to GameObjects with that name. */
			HashTable<String, Vector<GameObject*>> _game_object_map;
			

			//UNKNOWN USE
			Vector<IComponent*> _messaged_components;
			Vector<IDirector*> _messaged_directors;
			
			//a hash table of all prefabs this scene cares about,
			//key is the path to the ContentXML
			HashTable<String, Prefab*> _path_to_prefabs;
			//key is the key specified in the ContentXML
			HashTable<String, Prefab*> _key_to_prefabs;

			//hash table for the GUI Panels
			HashTable<String, GUIPanel*> _gui_panel_map;
		};
	}
}